﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using System.Windows.Forms;
using XCI.Core;
using XCI.Helper;
using XCI.Sys.Helper;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class FrmUnLock : FrmBase
    {
        public FrmUnLock()
        {
            InitializeComponent();
        }

        private bool IsExit { get; set; }

        private void btnUnLock_Click(object sender, EventArgs e)
        {
            string account = SysRuntime.CurrentUser.Account;
            string password = editPassword.Text;
            if (string.IsNullOrEmpty(password))
            {
                MessageBoxHelper.ShowWarn("请输入密码");
                editPassword.Select();
                return;
            }

            var captcha = string.Empty;
            var reqCap = UserService.Instance.RequireCaptcha(account);
            if (reqCap)
            {
                SplashHelper.ShowWait("正在生成验证码...", this);
                var capForm = new FrmCaptcha(account);
                capForm.ShowDialog();
                captcha = capForm.Captcha;
                capForm.Dispose();
            }

            var result = SysRuntime.App.UserValid(account, password, captcha);
            if (result.Success)
            {
                IsExit = true;
                this.DialogResult = DialogResult.OK;
            }
            else if (!string.IsNullOrEmpty(result.Msg))
            {
                MessageBoxHelper.ShowError(result.Msg);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            AppHelper.KillApplication();
        }

        private void UnLockMainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = !IsExit;
        }

        private void FrmMainUnLock_Load(object sender, EventArgs e)
        {
            //this.captionControl1.CaptionText = string.Format("当前用户:{0}",
            //    CurrentUser.Name);
            //NativeMethods.SetForegroundWindow(this.Handle);
        }
    }
}